package com.sky.loveshop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.loveshop.entity.User;
import com.sky.loveshop.entity.UserShopRelation;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.UserQO;
import com.sky.loveshop.entity.qo.UserRequest;
import com.sky.loveshop.entity.vo.UserVO;
import com.sky.loveshop.mapper.UserMapper;
import com.sky.loveshop.mapper.UserShopRelationMapper;
import com.sky.loveshop.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:06
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserShopRelationMapper userShopRelationMapper;

    @Override
    public List<UserVO> listUser(UserQO qo) {
        return userMapper.searchAll(qo);
    }

    @Override
    public PageInfo<UserVO> listUserPages(UserQO qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<UserVO> userList = userMapper.searchAll(qo);
        return new PageInfo<>(userList);
    }

    @Override
    public UserVO getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        UserVO userVO = new UserVO();
        List<UserShopRelation> usrList = getShopsByUserId(id);
        List<Integer> shopIds = null;
        if (CollectionUtil.isNotEmpty(usrList)) {
            shopIds = usrList.stream().map(UserShopRelation::getShopId).collect(Collectors.toList());
        }
        BeanUtils.copyProperties(user, userVO);
        userVO.setShopIds(shopIds);
        return userVO;
    }

    @Override
    public List<UserShopRelation> getShopsByUserId(Integer id) {
        return userShopRelationMapper.getShopsByUserId(id);
    }

    @Override
    public List<UserVO> getUserListByIds(List<Integer> ids) {
        List<User> userList = userMapper.getUserListByIds(ids);

        ArrayList<UserVO> userVOList = new ArrayList<>();

        userList.stream().forEach(s -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(s, userVO);
            userVOList.add(userVO);
        });
        return userVOList;
    }

    @Override
    public Integer addUser(User user) {
        user.setStatus(1);
        return userMapper.insertOne(user);
    }

    @Override
    public Integer delUserById(Integer id) {
        return userMapper.delUserById(id);
    }

    @Override
    public Integer updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public UserVO login(UserQO user) throws BizException {
        User u = userMapper.login(user);
        if (null == u) {
            throw new BizException(BizExceptionEnum.LOGIN_ERROR_PASSWORD);
        }
        if("admin-web".equals(user.getLogInFrom()) && !"admin".equals(u.getUserType())){
            throw new BizException(BizExceptionEnum.CAN_NOT_LOGIN_ADMIN_WEB);
        }
        if (u.getStatus() == 0) {
            throw new BizException(BizExceptionEnum.LOGIN_ERROR_STATUS);
        }

        List<UserShopRelation> usrList = getShopsByUserId(u.getId());
        List<Integer> shopIds = null;
        if (CollectionUtil.isNotEmpty(usrList)) {
            shopIds = usrList.stream().map(UserShopRelation::getShopId).collect(Collectors.toList());
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(u, userVO);
        userVO.setShopIds(shopIds);
        return userVO;
    }

    public Integer userBindShop(UserRequest.BindParam req) {
        List<Integer> shopIds = req.getShopIds();
        shopIds.stream().forEach(s -> {
            UserShopRelation r = new UserShopRelation();
            r.setUserId(req.getUserId());
            r.setShopId(s);
            userShopRelationMapper.insertOne(r);
        });
        return 1;
    }

    @Transactional
    @Override
    public Integer userBindShopUpdate(UserRequest.BindParam req) {
        // 先解绑
        userShopRelationMapper.delByUserId(req.getUserId());
        // 在绑定
        return userBindShop(req);
    }

    /**
     * 扣减用户的积分
     * @param userId 用户id
     * @param totalAmount 1积分=1元
     */
    @Override
    public Integer deducePoints(Integer userId, BigDecimal totalAmount) {
        return userMapper.deducePoints(userId, totalAmount);
    }
}
